package com.zxc.gulimall.member.service.impl;

import com.zxc.gulimall.member.dao.MemberLevelDao;
import com.zxc.gulimall.member.entity.MemberLevelEntity;
import com.zxc.gulimall.member.exception.PhoneExistsException;
import com.zxc.gulimall.member.exception.UsernameExistException;
import com.zxc.gulimall.member.vo.MemberLoginVo;
import com.zxc.gulimall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxc.common.utils.PageUtils;
import com.zxc.common.utils.Query;

import com.zxc.gulimall.member.dao.MemberDao;
import com.zxc.gulimall.member.entity.MemberEntity;
import com.zxc.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelDao memberLevelDao;

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String username = vo.getLoginAct();
        String password = vo.getPassword();

        //去数据库查询手机号或者用户名匹配
        MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username",vo.getLoginAct()).or().eq("mobile",vo.getLoginAct()));
        if (memberEntity == null){
            //登录失败
            return null;
        }else {
            //数据库的密码
            String passwordDB = memberEntity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(vo.getPassword(), passwordDB);

            if (matches){
                //密码匹配成功，登录成功
                return memberEntity;
            }else {
                //密码错误，登录失败
                return null;
            }

        }

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberRegisterVo vo) {
        MemberEntity memberEntity = new MemberEntity();
        //设置默认会员等级
        MemberLevelEntity memberLevelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(memberLevelEntity.getId());

        //检查用户名和手机号是否唯一，为了让controller能感知到异常，异常机制
        checkPhoneUnique(vo.getPhone());
        memberEntity.setMobile(vo.getPhone());
        checkUserNameUnique(vo.getUserName());
        memberEntity.setUsername(vo.getUserName());

        //密码加密存储
        memberEntity.setPassword(vo.getPassword());

        //盐值加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);

        //其他默认信息
        baseMapper.insert(memberEntity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistsException {
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count > 0){
            throw new PhoneExistsException();
        }
    }

    @Override
    public void checkUserNameUnique(String username) throws UsernameExistException {
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username",username));
        if (count > 0){
            throw new UsernameExistException();
        }
    }
}